﻿using System;
using System.ComponentModel;
using System.Data;
using ReflectionStudio.Core.Events;
using System.Windows;
using System.Linq;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class TGD723 : UIGAAUF
    {
        
        #region ==IsAB===========
        public static readonly DependencyProperty IsABProperty = DependencyProperty.Register("IsAB",
            typeof(bool), typeof(TGD723),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsABChanged));
        public bool IsAB
        {
            get
            {
                return (bool)GetValue(IsABProperty);
            }
            set
            {
                SetValue(IsABProperty, value);
            }
        }
        private static void OnIsABChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGD723;
            if (src != null)
            {
                src.IsAB = (bool)e.NewValue;
            }
        }
        #endregion

         #region ==IsGridShow===========
        public static readonly DependencyProperty IsGridShowProperty = DependencyProperty.Register("IsGridShow",
            typeof(bool), typeof(TGD723),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsGridShowChanged));
        public bool IsGridShow
        {
            get
            {
                return (bool)GetValue(IsGridShowProperty);
            }
            set
            {
                SetValue(IsGridShowProperty, value);
            }
        }
        private static void OnIsGridShowChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGD723;
            if (src != null)
            {
                src.IsGridShow = (bool)e.NewValue;
            }
        }
        #endregion
        
        public double QTYEND
        {
            get
            {
                if (dwRule == null)
                    return 1;
                return Convert.ToDouble(dwRule["QTYEND"].ToString());
            }

        }
        public double QTYMAT
        {
            get
            {
                if (dwRule == null)
                    return 0;
                return Convert.ToDouble(dwRule["QTYMAT"].ToString());
            } 

        }
        public TGD723()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VGAAUF();
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwGD == null)
            {
                CART = "";
                KEY = "";
                REQ = 0;
                ALLQ = 0;
            }
            else
            {
                REQ = (GAKG1 - GAKG2 - GAKG3 - GAKG5) / 100;
                ALLQ = GAKG3 + GAKG5;
                CART = dwGD["GAART"].ToString() + dwGD["GAVAR"].ToString() + dwGD["GAFARBE"].ToString() + dwGD["GAAUFMSL"].ToString();
                KEY = dwGD["GAAUFKRS"].ToString() + dwGD["GAAUFLFD"].ToString() + dwGD["GAAUFPOS"].ToString() + dwGD["GAPOS"].ToString() + POSTYP;
            }
        }

        public override void OrderCommand(object sender, RoutedEventArgs e)
        {
            PM["RUNID"] = RUNID;
            //根据不同点的原料类型，需求创建不同的订单
            //在创建时，决定使用现有库存还是新订单 
            if (dwGD == null)
                return;
            PM["ORDERNUM"] = dwGD["GAAUFKRS"].ToString() + dwGD["GAAUFLFD"].ToString() + dwGD["GAAUFPOS"].ToString() + dwGD["GASA"].ToString() + dwGD["GAPOS"].ToString(); ;
            //销售单规则已经创建好，这里捕捉实际值，并保存规则
            if (dwRule == null)
                return;
            if (dwRule["P3"].ToString() != "E" & dwRule["P3"].ToString() != "F" & dwRule["P3"].ToString() != "X")
            {
                return;
            }
            MessageInfo msg = new MessageInfo();
            double GSFACH=Convert.ToDouble((((VGAAUF)MV).P2));
            #region===产生ABKOPF======
            PM["NEWABK"] = "F";
            if (dwMU == null)
            {
                PM["ORDERNUM"] = "T";
                GSFACH = System.Convert.ToDouble(dwRule["P2"].ToString());
                if (GSFACH > 1)
                {
                    PM["KRS"] = "B" + MV.year.Substring(3, 1); ;
                }
                else
                {
                    PM["KRS"] = "X" + MV.year.Substring(3, 1); ;
                }
                PM["ORDERNUM"] = dwGD["GAAUFKRS"].ToString() + "-" + Helpers.Helper.Right(dwGD["GAAUFLFD"].ToString(), 4) + "-" + dwGD["GAAUFPOS"].ToString();
                PM["RUNID"] = this.RUNID;
                PM["AUFART"] = "GP";
                VABKOPF.Instance.NEWABK(PM);
            }
            if (dwABK == null)
                return;
            #endregion

            #region===产生首个计划规则，传递参数======
            msg = new MessageInfo();
            msg.Sender = this.RUNID;
            if (GSFACH > 1)
            {
                msg.MSGTYPE = "TWABPNEW";
                msg.ACTION = "TWABPNEW";
                VICC_PRULES.Instance.ARTSTAGE = "T";
                foreach (DataColumn cm in dwRule.Table.Columns)
                {
                    PM[cm.ColumnName] = dwRule[cm.ColumnName].ToString();
                }
                PM["ORDERNUM"] = dwABK["ABKAUFKRS"].ToString() + dwABK["ABKAUFLFD"].ToString() + "020" + "BX ";
            }
            else
            {
                msg.MSGTYPE = "XSABPNEW";
                msg.ACTION = "XSABPNEW";

                VICC_PRULES.Instance.ARTSTAGE = "S";
                foreach (DataColumn cm in dwRule.Table.Columns)
                {
                    PM[cm.ColumnName] = dwRule[cm.ColumnName].ToString();
                }
                PM["ORDERNUM"] = dwABK["ABKAUFKRS"].ToString() + dwABK["ABKAUFLFD"].ToString() + "020" + "XS ";
            }
            VICC_PRULES.Instance.NEWPLAN(PM);
            #endregion
            msg.AUFTYP = "K";
            msg.ART = dwGD["GAART"].ToString();
            msg.VAR = dwGD["GAVAR"].ToString();
            msg.FARBE = dwGD["GAFARBE"].ToString();
            msg.KRS = dwGD["GAAUFKRS"].ToString();
            msg.LFD = dwGD["GAAUFLFD"].ToString();
            msg.POS = dwGD["GAAUFPOS"].ToString();
            msg.P1 = dwGD["GAPOS"].ToString();
            msg.P2 = dwGD["GASA"].ToString();
            msg.P3 = (string)PM["NEWABK"];
            msg.ORDER = (string)PM["ORDERNUM"];
            msg.Where = app.User;
            VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
        }

        private void btnAddGAAUF_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                 if (dwGD == null)
                {
                    MessageBox.Show("请选择一条最相近的记录");
                    return;
                }
                dwData = TBMain.NewRow();
                dwData.ItemArray = dwGD.ItemArray;
                var gapos = (from DataRow row in TBMain.AsEnumerable()
                             select row["GAPOS"].ToString())
                             .Max();
                int POS = Convert.ToInt16((string)gapos);
                dwData["GAPOS"] = Helpers.Helper.Right("00" + (POS + 1), 2);
                ((VGAAUF)MV).Insert(dwData);
 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnDelGAAUF_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwGD == null)
                {
                    MessageBox.Show("请选择要删除的纱线");
                    return;
                }
                if (txtGADel.Text.ToString().Trim() != "Y")
                {
                    MessageBox.Show("必须手动设置后面标志为Y");
                    return;
                }
                VGLLG.Instance.RVZUTEI(dwGD);
                 ((VGAAUF)MV).DeleteGAAUF(dwGD, 1);
                string _KRS =dwGD["GAAUFKRS"].ToString();
                string _LFD =dwGD["GAAUFLFD"].ToString();
                string _POS =dwGD["GAAUFPOS"].ToString();
                string _aPOS= dwGD["GAPOS"].ToString();
                vwMaster.View.MoveCurrentTo(-1);
                string filter = "GAAUFKRS='" + _KRS + "' AND GAAUFLFD='" + _LFD + "' AND GAAUFPOS='" + _POS + "' AND GASA=713 AND GAPOS='" + _aPOS + "'";
                DataRow[] rows = TBMain.Select(filter);
                foreach (DataRow row in rows)
                { 
                    row.Delete();
                }
                TBMain.AcceptChanges();
                txtGADel.Text = "N";
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void Calculate_Click(object sender, RoutedEventArgs e)
        {
            try
            { 
                double Total = 0;
                //4.计算用量，GAKG1= ABPSOLLMENGE7*TargetRatio*GASP1,ABPSOLLMENGE5*ABPSTKLG
                //5.GABASMG2=GAKG1/ABPSOLLMENGE5
                foreach (DataRow dwGA in TBMain.Rows)
                {
                    dwGA["GAART"] = Helpers.Helper.Left(dwGA["GAART"].ToString().PadRight(20), 20);
                    dwGA["GAVAR"] = Helpers.Helper.Left(dwGA["GAVAR"].ToString().PadRight(3), 3);
                    dwGA["GAFARBE"] = Helpers.Helper.Left(dwGA["GAFARBE"].ToString().PadRight(10), 10);
                    dwGA["GABEM"] = Helpers.Helper.Left(dwGA["GABEM"].ToString().Trim().PadRight(40), 40);
                    dwGA["GAAUFMSL"] = Helpers.Helper.Left(dwGA["GAAUFMSL"].ToString().Trim().PadRight(2), 2);
                    
                    DataSet SResult = INTEXDBC.QueryGSCOLOR(dwGA, 5);
                    if (SResult.Tables[0].Rows.Count == 0)
                    {
                        throw new Exception("纱线:" + dwGA["GAART"].ToString() + dwGA["GAVAR"].ToString() + dwGA["GAFARBE"].ToString() + dwGA["GAAUFMSL"].ToString() + ",不存在，请核对编码");
                    }

                    string GASA = dwGA["GASA"].ToString();
                    if (GASA == "310") continue;
                    double GASP1 = Convert.ToDouble(dwGA["GASP1"].ToString()) * 0.001 * 0.01;
                    double MRatio = GASP1;
                    double GAKG1 = Math.Round(QTYMAT * MRatio * 100);
                    dwGA["GABASMG2"] = Math.Round(GAKG1 / QTYEND * 1000);
                    dwGA["GAKG1"] = GAKG1;
                    dwGA["GAKG3"] = 0;
                    dwGA["GAKG5"] = 0;
                    Total += MRatio;
                    ((VGAAUF)MV).UpdateGAAUF(dwGA, 0); 
                }
                if (Helpers.Helper.RoundX(Total*100,1) != 100)
                { 
                    MessageBox.Show("比例有误!");
                } 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
        #region=========
        #endregion
    }
}
